/**
 * 全局公用方法
 */
const calcCloseTime = function(closetime) {
	let seconds = closetime
	let [day, hour, minute, second] = [0, 0, 0, 0]
	let info = {
		day: 0,
		hour: 0,
		minute: 0,
		second: 0
	}
	let showTime = ''
	if (seconds > 0) {
		day = Math.floor(seconds / (60 * 60 * 24))
		hour = Math.floor(seconds / (60 * 60)) - (day * 24)
		minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
		second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
		// if (day < 10) {
		// 	day = '0' + day
		// }
		// if (hour < 10) {
		// 	hour = '0' + hour
		// }
		// if (minute < 10) {
		// 	minute = '0' + minute
		// }
		// if (second < 10) {
		// 	second = '0' + second
		// }
		if (day > 0) {
			showTime = day + '天'
		}
		if (hour > 0) {
			showTime = day + '天' + hour + '时'
		}
		if (minute > 0) {
			showTime = day + '天' + hour + '时' + minute + '分'
		}
		
		info = {
			day: day,
			hour: hour,
			minute: minute,
			second: second,
			time: showTime
		}
	}
	return info
}

const timestampToTime = function(cjsj, type) {
	
	console.log('0000',type)
	let spa = '-'
	var date = new Date(cjsj * 1000) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + spa
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + spa
	var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' '
	
	let h = date.getHours()<10?'0'+date.getHours():date.getHours()
	let m = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()
	let s = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()
	
	if(type == 'all'){
		return Y + M + D+ ' '+ h +':' + m +':' + s
	}else{
		return Y + M + D
	}
	
	// 
}

const timeToTimestamp = function(time) {
	return (new Date(time)).getTime() / 1000
	
}
const backToPrePage = function() {

	let pages = getCurrentPages()
	let pagesLength = pages.length

	if (pagesLength == 1) {
		//第一页 返回首页
		uni.switchTab({
			url: '/pages/tabbar/home/home'
		})
	} else {
		//返回上一页
		uni.navigateBack({
			delta: 1
		});
	}

}
const checkNewVersion = function() {
	//检查版本更新
	const updateManager = uni.getUpdateManager();
	updateManager.onCheckForUpdate(function(res) {
		if (res.hasUpdate) {
			updateManager.onUpdateReady(function() {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，点击确定重新启动',
					showCancel: true,
					success: res => {
						if (res.confirm) {
							updateManager.applyUpdate();
						}
					}
				})
			})
			
			updateManager.onUpdateFailed(function() {
				uni.showModal({
					title: '提示',
					content: '检查到有新版本，但是下载失败，请检查网络设置',
					showCancel: false
				})
			})
		}
	})
}


const getLocationInfo = function(res) {
	var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
	var REGION_PROVINCE = [];
	var addressBean = {
		REGION_PROVINCE: null,
		REGION_COUNTRY: null,
		REGION_CITY: null,
		ADDRESS: null
	};

	function regexAddressBean(address, addressBean) {
		regex = /^(.*?[市州]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
		var addxress = regex.exec(address);
		addressBean.REGION_CITY = addxress[1];
		addressBean.REGION_COUNTRY = addxress[2];
		// addressBean.ADDRESS = addxress[3] + "(" + res.name + ")";
		addressBean.ADDRESS = res.name
		console.log(addxress);
	}
	if (!(REGION_PROVINCE = regex.exec(res.address))) {
		regex = /^(.*?(省|自治区))(.*?)$/;
		REGION_PROVINCE = regex.exec(res.address);
		addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
		regexAddressBean(REGION_PROVINCE[3], addressBean);
	} else {
		addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
		regexAddressBean(res.address, addressBean);
	}
	let areaInfo = addressBean.REGION_PROVINCE + addressBean.REGION_CITY + addressBean.REGION_COUNTRY
	if (addressBean.REGION_PROVINCE == addressBean.REGION_CITY) {
		areaInfo = addressBean.REGION_PROVINCE + addressBean.REGION_COUNTRY
	}
	addressBean.AREA_INFO = areaInfo
	return addressBean
}

const urlToBase64 = function(url) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: 'GET',
			responseType: 'arraybuffer',
			success: res => {
				let base64 = wx.arrayBufferToBase64(res.data); //把arraybuffer转成base64
				base64 = 'data:image/jpeg;base64,' + base64; //不加上这串字符，在页面无法显示
				console.log(base64);
				resolve(base64)

			}
		});

	})

}
const loginFailure = function() {
	let pages = getCurrentPages()
	var page = pages[pages.length - 1];
	console.log('page', page.route)
	if (page.route == 'pages/login/index') {
		//如果已经在登录页 不做跳转 4be73c3f-9619-48d7-885f-ce5802f10d90
		console.log('enter')
		return
	}

	uni.showToast({
		icon: "none",
		title: '登录失效，请重新登录'
	})
	uni.removeStorageSync('token')
	uni.setStorageSync('isLogin',false)

	uni.reLaunch({
		url: "/pages/login/index"
	})
}
const getNetworkType=function(){
	uni.getNetworkType({
		success(res) {
			let networkType = res.networkType
			console.log('networkType',networkType)
			if(networkType != 'wifi' && networkType != '4g'){
				uni.showModal({
					content:'您的网络状况不好，请更换网络后重试！',
					showCancel:false,
					confirmColor:'#0BB976'
				})
			}
		}
	})
}
export default {
	timestampToTime,
	timeToTimestamp,
	getLocationInfo,
	calcCloseTime,
	backToPrePage,
	urlToBase64,
	loginFailure,
	checkNewVersion,
	getNetworkType
}
